<template>
    <base-breadcrumb :breadcrumb="!isAudit">
        <template>
            <a-card class="mb20">
                <titleName title="基础信息填写" />
                <a-form-model
                    ref="ruleForm"
                    :model="form"
                    :rules="rules"
                    :label-col="{ lg: { span: 6 }, sm: { span: 6 } }"
                    :wrapper-col="{ lg: { span: 16 }, sm: { span: 16 } }"
                >
                    <a-row :gutter="24">
                            <a-col :md="16" :sm="16">
                                    <a-form-model-item label="所属单位" prop="projectId" :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }" :wrapperCol="{ lg: { span: 16 }, sm: { span: 16 } }">
                                        <org-tree-select v-model="form.projectId" disabled
                                                         checkStrictly @select="selectOrg"></org-tree-select>
                                    </a-form-model-item>
                            </a-col>
                        <!-- <a-col :md="8" :sm="8">
                            <a-form-model-item label="所属分公司：" prop="branchComId">
                                <SelectSubCompany
                                    v-model="form.branchComId"
                                    :disabled="type == '0'"
                                    placeholder="请选择"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="所属项目：" prop="projectId">
                                <SelectProject
                                    v-model="form.projectId"
                                    :disabled="type == '0'"
                                    placeholder="请选择"
                                    :form="form"
                                    :is-clear="id == ''"
                                    key-value="branchComId"
                                    @selectChange="(e) => (form.name = e.name + '总包结算目标责任书')"
                                    @change="getProjectInfo"
                                />
                            </a-form-model-item>
                        </a-col> -->

                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="名称：" prop="name">
                                <BaseInput v-model="form.name" :disabled="type == '0'" placeholder="请输入名称" />
                            </a-form-model-item>
                        </a-col>

                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="建筑面积(㎡)：" prop="constructionArea">
                                <BaseInput
                                    v-model="form.constructionArea"
                                    :disabled="type == '0'"
                                    placeholder="请输入"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="合同开工日期：" prop="contractStartDate">
                                <a-date-picker
                                    v-model="form.contractStartDate"
                                    :disabled="type == '0'"
                                    @change="
                                        (date, dateString) => getTime(date, dateString, 'form', 'contractStartDate')
                                    "
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="合同竣工日期：" prop="contractEndDate">
                                <a-date-picker
                                    v-model="form.contractEndDate"
                                    :disabled="type == '0'"
                                    @change="(date, dateString) => getTime(date, dateString, 'form', 'contractEndDate')"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="24" :sm="24" class="title"> 合同额（万元） </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="不含税：" prop="contractTaxExclusive">
                                <BaseInputNumber
                                    v-model="form.contractTaxExclusive"
                                    :max="99999999999999"
                                    :disabled="type == '0'"
                                    :min="0"
                                    placeholder="请输入"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="增值税：" prop="contractAddedValueTax">
                                <BaseInputNumber
                                    v-model="form.contractAddedValueTax"
                                    :max="99999999999999"
                                    :disabled="type == '0'"
                                    :min="0"
                                    placeholder="请输入"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="含税：">
                                <BaseInputNumber
                                    :max="99999999999999"
                                    :min="0"
                                    disabled
                                    :precision="2"
                                    placeholder="请输入"
                                    :value="
                                        form.contractTaxExclusive + form.contractAddedValueTax > 0
                                            ? form.contractTaxExclusive + form.contractAddedValueTax
                                            : 0
                                    "
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="24" :sm="24" class="title"> 自施合同额（万元） </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="不含税：" prop="roadworkTaxExclusive">
                                <BaseInputNumber
                                    v-model="form.roadworkTaxExclusive"
                                    :max="99999999999999"
                                    :disabled="type == '0'"
                                    :min="0"
                                    placeholder="请输入"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="增值税：" prop="roadworkAddedValueTax">
                                <BaseInputNumber
                                    v-model="form.roadworkAddedValueTax"
                                    :max="99999999999999"
                                    :disabled="type == '0'"
                                    :min="0"
                                    placeholder="请输入"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="含税：">
                                <BaseInputNumber
                                    :max="99999999999999"
                                    disabled
                                    placeholder="请输入"
                                    :precision="2"
                                    :value="
                                        form.roadworkTaxExclusive + form.roadworkAddedValueTax > 0
                                            ? form.roadworkTaxExclusive + form.roadworkAddedValueTax
                                            : 0
                                    "
                                />
                            </a-form-model-item>
                        </a-col>
                        <!-- <a-col :md="24" :sm="24" class="title">总包结算保底值（万元）</a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="不含税：" prop="securityTaxExclusive">
                                <BaseInputNumber
                                    :max="99999999999999"
                                    :disabled="type == '0'"
                                    :min="0"
                                    placeholder="请输入"
                                    v-model="form.securityTaxExclusive"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="增值税：" prop="securityAddedValueTax">
                                <BaseInputNumber
                                    :max="99999999999999"
                                    :disabled="type == '0'"
                                    :min="0"
                                    placeholder="请输入"
                                    v-model="form.securityAddedValueTax"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="含税：">
                                <BaseInputNumber
                                    :max="99999999999999"
                                    :min="0"
                                    disabled
                                    placeholder="请输入"
                                    :precision="2"
                                    :value="
                                        form.securityTaxExclusive + form.securityAddedValueTax > 0
                                            ? form.securityTaxExclusive + form.securityAddedValueTax
                                            : 0
                                    "
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="24" :sm="24" class="title">总包结算目标值（万元）</a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="不含税：" prop="targetTaxExclusive">
                                <BaseInputNumber
                                    :max="99999999999999"
                                    :disabled="type == '0'"
                                    :min="0"
                                    placeholder="请输入"
                                    v-model="form.targetTaxExclusive"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="增值税：" prop="targetAddedValueTax">
                                <BaseInputNumber
                                    :max="99999999999999"
                                    :disabled="type == '0'"
                                    :min="0"
                                    placeholder="请输入"
                                    v-model="form.targetAddedValueTax"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="含税：">
                                <BaseInputNumber
                                    :max="99999999999999"
                                    :min="0"
                                    disabled
                                    placeholder="请输入"
                                    :precision="2"
                                    :value="
                                        form.targetTaxExclusive + form.targetAddedValueTax > 0
                                            ? form.targetTaxExclusive + form.targetAddedValueTax
                                            : 0
                                    "
                                />
                            </a-form-model-item>
                        </a-col> -->
                        <a-col :md="24" :sm="24" class="title"> 总包结算目标 </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="考核指标类型" prop="evaluationIndexTypeId">
                                <a-select v-model="form.evaluationIndexTypeId" :disabled="type == '0'" placeholder="">
                                    <a-select-option v-for="i in selectList" :key="i.id" :value="i.id">
                                        {{ i.name }}
                                    </a-select-option>
                                </a-select>
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="结算责任上缴利润率（%）" prop="respRate">
                                <a-input-number v-model="form.respRate" :max="1000" :precision="2" placeholder="请输入"></a-input-number>
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item label="签订完成时间" prop="compleDate">

                                <a-date-picker
                                        v-model="form.compleDate"
                                        value-format="YYYY-MM-DD"
                                        placeholder="请选择"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="24" :sm="24">
                            <a-form-model-item label="考核指标" prop="evaluationIndex">
                                <BaseTextarea
                                    v-model="form.evaluationIndex"
                                    :disabled="type == '0'"
                                    placeholder="请输入"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="24" :sm="24">
                            <a-form-model-item label="结算完成时间" prop="settlementCompleteTime">
                                <BaseTextarea
                                    v-model="form.settlementCompleteTime"
                                    :disabled="type == '0'"
                                    placeholder="请输入"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col v-if="type != '0'" :md="16" :sm="6">
                            <a-form-model-item label="附件上传">
                                <l-table
                                    v-if="isUpdate"
                                    :upload-arrys="form.fileInfoVOList"
                                    business-code="BM04"
                                    @fileArrysData="fileArrysData"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col v-else :md="16" :sm="6">
                            <a-form-model-item label="附件：">
                                <file-link :file-list="form.fileInfoVOList" />
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                </a-form-model>
            </a-card>

            <div v-if="type != '0'" class="tool-btns mb20 page-btn-right-top">
<!--                <a-button-->
<!--                    v-if="id != '' && form.auditStatus == 'DRAFT'"-->
<!--                    :loading="loading.DELETE"-->
<!--                    type="danger"-->
<!--                    @click="deleteDraft"-->
<!--                >-->
<!--                    删除草稿-->
<!--                </a-button>-->
<!--                <a-button-->
<!--                    v-if="form.auditStatus == 'REJECTED'"-->
<!--                    type="primary"-->
<!--                    :loading="loading.REJECTED"-->
<!--                    @click="submitForm('REJECTED')"-->
<!--                >-->
<!--                    保存-->
<!--                </a-button>-->
                <a-button :loading="loading.DRAFT" type="primary" @click="submitForm('DRAFT')">
                    保存草稿
                </a-button>
                <a-button :loading="loading.SUBMITTED" type="primary" @click="submitForm('SUBMITTED')"> 提交 </a-button>
                <a-button type="primary" @click="closeForm"> 关闭 </a-button>
            </div>
            <div v-else class="tool-btns mb20 page-btn-right-top">
                <a-button v-if="checkVisibleInDetail" type="primary" @click="closeForm"> 关闭 </a-button>
            </div>
        </template>
       <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
    </base-breadcrumb>
</template>
<script>
import {
    addProjectSettlement,
    getProjectSettlement,
    updateProjectSettlement,
    deleteProjectSettlement,
    getProjectInfo,
    getSettlementPlanningList
} from '@/api/businessAffairs/revenueAndCost'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
import { isAttachment, checkVisibleInDetail } from '@/utils/util'
import Vue from 'vue'
import { getSelectListByType } from '@/api/businessAffairs/common.js'
export default {
    name: 'a' + Date.now(),
    components: {
        SelectSubCompany,
        SelectProject
    },
    props: {
        isAudit: {
            // 是否作为审批详情
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            checkVisibleInDetail: checkVisibleInDetail(this),
            form: { constructionArea: '', name: '', compleDate: '', respRate: '' },
            cacheForm: {},
            rules: {
                branchComId: [{ required: true, message: '请选择分公司' }],
                projectId: [{ required: true, message: '请选择项目' }],
                name: [{ required: true, message: '请输入名称' }],
                compleDate: [{ required: true, message: '请选择签订完成时间' }],
                respRate: [{ required: true, message: '请输入结算责任上缴利润率' }],
                constructionArea: [{ required: true, message: '请输入建筑面积' }],
                contractStartDate: [{ required: true, message: '请输入合同开工日期' }],
                contractEndDate: [{ required: true, message: '请输入合同竣工日期' }],
                contractTaxExclusive: [{ required: true, message: '请输入不含税' }],
                contractAddedValueTax: [{ required: true, message: '请输入增值税' }],

                roadworkTaxExclusive: [{ required: true, message: '请输入不含税' }],
                roadworkAddedValueTax: [{ required: true, message: '请输入不含税' }],

                securityTaxExclusive: [{ required: true, message: '请输入不含税' }],
                securityAddedValueTax: [{ required: true, message: '请输入不含税' }],

                targetTaxExclusive: [{ required: true, message: '请输入不含税' }],
                targetAddedValueTax: [{ required: true, message: '请输入不含税' }],

                evaluationIndexTypeId: [{ required: true, message: '请选择考核指标类型' }],
                evaluationIndex: [{ required: true, message: '请输入考核指标' }],
                settlementCompleteTime: [{ required: true, message: '请输入结算完成时间' }]
            },
            loading: {
                DELETE: false,
                DRAFT: false,
                SUBMITTED: false,
                REJECTED: false
            },
            planningBooks: [],
            id: '',
            isUpdate: true,
            type: '',
            token: '',
            selectList: [],
           //获取流程配置值
            processConfig: null,
            //点击提交发送流程配置值
            setProcessConfig: null,
            businessId: null
        }
    },
    watch: {
        isAudit: {
            immediate: true,
            handler(val) {
                if (val) {
                    this.$nextTick(() => {
                        this.type = '0'
                    })
                }
            }
        }
    },
   async mounted() {
        this.token = Vue.getAcloudProvider().getAccessToken()
        if (this.$route.query.id) {
            this.id = this.$route.query.id
            console.log(this.id)
            this.getProjectSettlement()
        }
        this.type = this.$route.query.type
         //获取通过获取流程配置值
       this.processConfig = await this.$getConfigFn('comm_general_settlement_responsibility_audit_new')
    },
    created() {
        this.getSelectListByType({ type: 19 })
    },
    methods: {
        //选择单位
        selectOrg(val) {
            if (val && val.length) {
                this.form.branchComId = val[val.length - 2].orgId;
                // this.getProjectInfo(this.form.projectId)
            }
        },
        async getSelectListByType(params) {
            const [err, res] = await getSelectListByType(params)
            if (!err) {
                res.data.forEach((item) => {
                    item.value = item.id
                    item.label = item.name
                })
                this.selectList = res.data
            }
        },
        getTime(date, dateString, type, name) {
            this[type][name] = dateString
            console.log(this[type][name])
        },
        // //获取项目详情
        // handleChange(e) {
        //     console.log(e)
        //     if (!e) return
        //     this.$nextTick(() => {
        //         this.form.name = this.form.project.label + '总包结算目标责任书'
        //         this.getProjectInfo(e.key)
        //     })
        // },
        getSettlementPlanningList(id) {
            let params = {
                projectId: id,
                pageNo: 1,
                pageSize: 10
            }
            getSettlementPlanningList(params).then((res) => {
                if (res.code == '200') {
                    this.planningBooks = res.data
                    this.showTip()
                }
            })
        },
        showTip() {
            if (this.planningBooks.length == 0 && this.type != '0') {
                this.$message.warning('请添加该项目的目标责任书！')
                return true
            }
            return false
        },
        getProjectInfo(id) {
            this.getSettlementPlanningList(id)
            let params = {
                projectId: id
            }
            getProjectInfo(params).then((res) => {
                this.$set(this.form, 'constructionArea', res.data.contractTotalArea * 100 * 100)
            })
        },
        fileArrysData: function (val) {
            this.form.fileInfoVOList = val
            console.log(val)
        },
        getProjectSettlement() {
            let params = {
                id: this.id
            }
            this.isUpdate = false
            getProjectSettlement(params).then((res) => {
                this.isUpdate = true

                this.form = { ...res.data }
                if(!this.form.name) {
                    this.form.name = this.form.projectName + '结算责任书'
                }
                if(!this.form.constructionArea) {
                    this.getProjectInfo(this.form.projectId)
                }
            })
        },
        // 删除草稿
        deleteDraft() {
            let that = this
            that.$confirm({
                title: '温馨提示',
                content: '确定是否删除数据？',
                okText: '确认',
                okType: 'danger',
                cancelText: '取消',
                onOk() {
                    return deleteProjectSettlement({id:that.form.id,type:1})
                        .then((res) => {
                            that.$message.success('删除成功！')
                            that.closeForm()
                        })
                        .catch((res) => {
                            that.$message.warning('删除失败！')
                        })
                }
            })
        },

        // 提交表单
        submitForm(auditStatus) {
            this.setProcessConfig = null;
            // if (this.showTip()) return

            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    if (isAttachment(this, auditStatus, this.form.fileInfoVOList)) return
                    this.form.fileIdList = this.form.fileInfoVOList

                    this.loading[auditStatus] = true
                    this.form.auditStatus = auditStatus
                    this.submitApprovalConfirm(auditStatus =='SUBMITTED'?this.processConfig:6, (flag)=>{
                      if(!flag) {
                        this.loading[auditStatus] = false
                        return
                      }
                    if (this.id == '') {
                        addProjectSettlement(this.form).then((res) => {
                            if (res.code == '200') {
                               if(auditStatus =='SUBMITTED') {
                                 this.$message.success('提交成功')
                                //提交时，返回id
                                    this.businessId = res.data.id;
                                    this.setProcessConfig = this.processConfig;
                                    return
                                }else{
                                 this.$message.success('保存草稿成功')
                                }
                              this.closeForm()
                            }
                        })
                    } else {
                        updateProjectSettlement(this.form).then((res) => {
                            if (res.code == '200') {
                                this.$message.success(auditStatus === 'SUBMITTED' ? '提交成功' : '保存草稿成功')
                                if (auditStatus =='SUBMITTED') {
                                   //提交时，返回id
                                    this.businessId = res.data.id || this.id;
                                    this.setProcessConfig = this.processConfig;
                                    return
                                }else{
                                this.closeForm()
                                }
                            }
                        })
                    }
                    })
                } else {
                  this.$message.warning('请填写完所有必填项信息')
                    console.log('error submit!!')
                    return false
                }
            })

            //
        },
        // 关闭表单
        closeForm() {
            if (this.isAudit) {
                window.close()
            } else {
                this.$multiTab.close(this.$route.fullpath)
            }
        }
    }
}
</script>
<style lang="less" scoped>
@deep: ~'>>>';
.tool-btns {
    display: flex;
    justify-content: flex-end;
    @{deep} .ant-btn + .ant-btn {
        margin-left: 12px;
    }
}
.step-text {
    .ant-steps-item-title {
        width: 168px;
    }
}
.check-box {
    .ant-checkbox-group {
        margin-top: 1.5%;
        display: flex;
        justify-content: space-between;
    }
    .ant-checkbox-group-item {
        position: static !important;
    }
}
.radio-box {
    .ant-radio-wrapper {
        position: static !important;
    }
    .ant-radio-group {
        margin-top: 3% !important;
        display: flex;
    }
}
.title {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    text-align: left;
    font-weight: bold;
    margin-bottom: 24px;
}

.foot-btn {
    display: flex;
    justify-content: flex-end;
}
</style>
